package com.dongdongshop.controller;

import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("logenController")
public class LogenController {

    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("listGet")
    @ResponseBody
    public SpecificPrint listGet(String sellerId, String password, Model model){
        try {
            //使用shiro编写登录逻辑
            //1.获取subiect
            Subject subject = SecurityUtils.getSubject();
            //2.将参数封装成token对象
            UsernamePasswordToken token = new UsernamePasswordToken(sellerId,password);

            try {
                //3.执行登录逻辑
                subject.login(token); //帮我们执行登录逻辑
                return SpecificPrint.response(ResultEnum.SUCCESS);
            }catch (UnknownAccountException e){//账号（用户）不存在
                return SpecificPrint.response(ResultEnum.NOTLOGGIN);
            }catch (IncorrectCredentialsException e){ //表示密码不正确
                return SpecificPrint.response(ResultEnum.UNAUTHORIZED);
            }
        }catch (Exception e){
            return SpecificPrint.response(ResultEnum.AUDITERRORS);
        }

    }

    @RequestMapping("unauthorized")
    public String unauthorized(){
        return "unauthorized";
    }

}
